﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using TianYu.CMS.Domain.ViewModel.Request;
using TianYu.CMS.Service;
using TianYu.Core.Common;

namespace TianYu.CMS.WebMvc.Areas.Admin.Controllers
{
    public class HomeController : BaseController
    {
        private ISysMenuService _sysMenuService;
        private ISysConfigService _sysConfigService;

        public HomeController(ISysMenuService sysMenuService, ISysConfigService sysConfigService)
        {
            this._sysMenuService = sysMenuService;
            this._sysConfigService = sysConfigService;
        }



        public async Task<IActionResult> Index()
        {
            var menuList = await _sysMenuService.FindUserMenu(base.LoginUserGuid);

            ViewBag.LoginName = HttpContext.Session.GetString("Admin_UserName");

            var userButtonListStr = string.Empty;

            if (menuList != null)
            {
                var userButtonList = menuList.Where(x => x.GroupId > 0);
                userButtonListStr = JsonHelper.SerializeObject(userButtonList);
            }
            HttpContext.Session.SetString("Admin_UserButtonListStr", userButtonListStr);

            var config = _sysConfigService.FindByClause(o => o.ConfigCode == "baseConfig");

            if (config != null)
            {
                if (!config.ConfigContent.IsNullOrWhiteSpace())
                {
                    var entity = JsonHelper.DeserializeObject<SysConfigViewMode>(config.ConfigContent);

                    ViewBag.Title = entity.SysName;
                }
            }

            return View(menuList);
        }

        public IActionResult Main()
        {
            return View();
        }
    }
}
